home *** CD-ROM | disk | FTP | other *** search
/ Gekkan Dennou Club 140 / Gekkan Dennou Club - 2000.1 Vol. 140 (Japan).7z / Gekkan Dennou Club - 2000.1 Vol. 140 (Japan) (Track 1).bin / tools / dshell / dsh333bs.lzh / find.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-11  |  5.7 KB  |  323 lines

  1. /*
  2.     dshell    v3
  3.  
  4.     文字列検索
  5. */
  6.  
  7.  
  8. #include    "dsh.h"
  9.  
  10.  
  11. static int Inc_search(int *, char *);
  12. static int Dec_search(int *, char *);
  13. static int Search_Exec(char *);
  14. static int underlin(int );
  15.  
  16.  
  17.  
  18. void 
  19. Menu_find(void)
  20. {
  21.     struct INPPTR inp;
  22.     int INPLEN  = (GMODE) ? 48 : 64;
  23.  
  24.     getstr(&inp, "検索を行なう文字列を入力して下さい(リターンのみで中止)", INPLEN);
  25.  
  26.     /* *****
  27.     !    B_LOCATE( INP_SX,INP_SY+1 );
  28.     !    printf("input string = %s",search_string);
  29.     ***** */
  30.  
  31.     /*
  32.     !    検索文字が入力されたか?
  33.     */
  34.     if (inp.length == 0) {
  35.         wait_mb_off();
  36.         return;
  37.     }
  38.     /*
  39.     !    検索実行へ
  40.     */
  41.     Search_Exec(inp.buffer);
  42.  
  43.     /*
  44.     !    画面の復帰
  45.     */
  46.     msarea(0, 0, GWIDTH - 1, 511);
  47.     p_scr();
  48.     wait_mb_off();
  49. }
  50.  
  51. /*
  52.     サーチ選択メニュー用構造体
  53.     (なんかあまりウツクシクない…)
  54. */
  55. struct ITEM {
  56.     int posx;        /* 表示開始x位置(offset:chr) init_...デ初期化スルノ */
  57.     int meslen;        /* 項目文字列長さ(chr) init_...デ初期化スルノ */
  58.     char *mes;        /* メニュー項目 */
  59. };
  60.  
  61. struct MENU {
  62.     int sx;            /* メニュー表示開始x座標(chr) */
  63.     int sy;            /* メニュー表示開始y座標(chr) */
  64.     int lx;            /* メニューx長さ(chr) */
  65.     int ly;            /* メニューy長さ(chr) */
  66.     int in;            /* 項目数 */
  67.     struct ITEM *items;    /* 項目実体 */
  68. };
  69.  
  70. /*
  71.     検索実行
  72. */
  73. static int 
  74. Search_Exec(char *string)
  75. {
  76.     int i;
  77.     int dmx, dmy, mx, my, mbl, mbr;
  78.     int tx, otx;
  79.     int curlin;        /* 検索現在(検索開始)行(0<=curlin<lpmx) */
  80.     int flin;        /* 表示上の現在行(0<=flin<30):下線引いて目立たせるため */
  81.  
  82.     static void init_SrchMenu(struct MENU *);
  83.     static void display_menu(struct MENU *);
  84.     static void display_item(struct MENU *, int, int);
  85.     static int check_select_menu(int, struct MENU *);
  86.  
  87.  
  88.     struct ITEM SMItem[]=
  89.     {
  90.         {0, 0, "前方検索"},
  91.         {0, 0, "後方検索"},
  92.         {0, 0, "[終了]"}
  93.     };
  94.  
  95. #define    ITEMN    ( sizeof( SMItem ) / sizeof( struct ITEM ) )    /* 項目数:3 */
  96. #define    WIN_SY        29    /*    同上??    */
  97.     struct MENU SrchMenu =
  98.     {
  99.         0,        /* メニュー表示開始x座標(dummy) */
  100.         WIN_SY,        /* メニュー表示開始y座標 */
  101.         0,        /* メニューx長さ(init_...デ初期化スルノ) */
  102.         1,        /* メニューy長さ(横長メニューナノネ) */
  103.         ITEMN,        /* アイテム数 */
  104.         SMItem        /* 項目実体 */
  105.     };
  106.  
  107.     SrchMenu.sx = CWIDTH - 34;
  108.  
  109.  
  110.  
  111.     init_SrchMenu(&SrchMenu);
  112.  
  113.     display_menu(&SrchMenu);
  114.  
  115.     /*
  116.     !    セレクト(検索開始)
  117.     */
  118.     otx = -1;
  119.     curlin = lp;        /* 検索文字列更新の都度、 curlin も更新される */
  120.     while (1) {
  121.         dmsstat(&dmx, &dmy, &mbl, &mbr);
  122.         dmspos(&mx, &my);
  123.         p_time(0);
  124.         /*
  125.         !    右ボタン(キャンセル)
  126.         */
  127.         if (mbr) {
  128.             break;
  129.         }
  130.         /*
  131.         !    左ボタン(決定)
  132.         */
  133.         if (otx != -1 && mbl) {
  134.             switch (tx) {
  135.             case 0:
  136.                 flin = Inc_search(&curlin, string);
  137.                 break;
  138.             case 1:
  139.                 flin = Dec_search(&curlin, string);
  140.                 break;
  141.             case 2:
  142.                 return (0);
  143.             default:
  144.                 break;
  145.             }
  146.             p_scr();
  147.             p_fpt(1);
  148.             if (flin >= 0) {
  149.                 underlin(flin);
  150.             }
  151.             display_menu(&SrchMenu);
  152.             otx = -1;
  153.             wait_mb_off();
  154.         }
  155.         /*
  156.         !    メニュー選択チェック
  157.         */
  158.         i = check_select_menu(mx, &SrchMenu);
  159.         if (i >= 0 && i != otx) {
  160.             tx = i;
  161.             display_item(&SrchMenu, otx, 3);    /* 白色 */
  162.             display_item(&SrchMenu, tx, 13);    /* 水色反転 */
  163.             otx = tx;
  164.         }
  165.         /*        otx=tx;    */
  166.     }
  167.  
  168.     return (1);
  169. }
  170.  
  171. /*
  172.     前方検索
  173.     clin 更新:
  174.     見つかった場合、
  175.     lp も更新、表示上の該当行を返す
  176.     見つからなかった場合は-1を返す
  177. */
  178. static int 
  179. Inc_search(int *clin, char *string)
  180. {
  181.     if (++(*clin) >= lpmx) {
  182.         --(*clin);
  183.     }
  184.     for (; *clin < lpmx; ++(*clin)) {
  185.         if (dinstr(lhp[*clin], string) != 0) {
  186.             lp = *clin - 15;
  187.             if (lp + 30 >= lpmx) {
  188.                 lp = lpmx - 30;
  189.             }
  190.             if (lp < 0) {
  191.                 lp = 0;
  192.             }
  193.             return (*clin - lp);
  194.         }
  195.     }
  196.     return (-1);
  197. }
  198.  
  199. /*
  200.     後方検索
  201.     clin 更新:
  202.     見つかった場合、
  203.     lp も更新、表示上の該当行を返す
  204.     見つからなかった場合は-1を返す
  205. */
  206. static int 
  207. Dec_search(int *clin, char *string)
  208. {
  209.     if (--(*clin) < 0) {
  210.         ++(*clin);
  211.     }
  212.     for (; *clin >= 0; --(*clin)) {
  213.         if (dinstr(lhp[*clin], string) != 0) {
  214.             lp = *clin - 15;
  215.             if (lp + 30 >= lpmx) {
  216.                 lp = lpmx - 30;
  217.             }
  218.             if (lp < 0) {
  219.                 lp = 0;
  220.             }
  221.             return (*clin - lp);
  222.         }
  223.     }
  224.     return (-1);
  225. }
  226.  
  227. /*
  228.     マウスカーソルのエリアなど初期化
  229. */
  230. static void
  231. init_SrchMenu(struct MENU *menu)
  232. {
  233.     int i, l;
  234.  
  235.     /* posxとmeslenとmenu->lxを計算 */
  236.  
  237.     l = 2;
  238.     for (i = 0; i < menu->in; i++) {
  239.         menu->items[i].posx = l;
  240.         menu->items[i].meslen = strlen(menu->items[i].mes);
  241.         l += menu->items[i].meslen;
  242.         l += 2;
  243.     }
  244.     menu->lx = l;
  245.     /*
  246.         マウスカーソルエリア固定
  247.     */
  248.     msarea(menu->sx * 8, menu->sy * 16, (menu->sx + menu->lx) * 8, (menu->sy + menu->ly) * 16);
  249. }
  250.  
  251. /*
  252. !    選択メニュー表示
  253. */
  254. static void
  255. display_item(struct MENU *menu, int no, int color)
  256. {
  257.     if (no >= 0 && no < menu->in) {
  258.         B_LOCATE(menu->sx + menu->items[no].posx, menu->sy);
  259.         B_COLOR(color);
  260.         B_PRINT(menu->items[no].mes);
  261.         B_COLOR(3);    /* normal color */
  262.     }
  263. }
  264.  
  265. static void
  266. display_menu(struct MENU *menu)
  267. {
  268.     int i;
  269.  
  270.     tbox_w2(menu->sx, menu->sy, menu->sx + menu->lx, menu->sy + menu->ly);
  271.     for (i = 0; i < menu->in; i++) {
  272.         display_item(menu, i, 3);
  273.     }
  274. }
  275.  
  276.  
  277. /*
  278.     メニュー選択判定(カーソル当たり判定?)
  279.  
  280.     mx:マウスカーソルのx位置(dot)
  281. */
  282. static int
  283. check_select_menu(int mx, struct MENU *menu)
  284. {
  285.     int i, tmpx;
  286.  
  287.     mx /= 8;
  288.     for (i = 0; i < menu->in; i++) {
  289.         tmpx = menu->sx + menu->items[i].posx;
  290.         if (mx >= tmpx && mx <= (tmpx + menu->items[i].meslen)) {
  291.             return (i);
  292.         }
  293.     }
  294.     return (-1);
  295. }
  296.  
  297. /*
  298.     アンダーラインを引く
  299.     汎用性のカケラもない関数じゃ…
  300. */
  301. static int
  302. underlin(int lin)
  303. {
  304.     struct XLINEPTR txxptr;
  305.  
  306.     txxptr.vram_page = 1;    /* vram_page */
  307.     txxptr.x = 0;        /* x */
  308.     txxptr.y = 0;        /* y */
  309.     txxptr.x1 = 768;    /* x1 */
  310.     txxptr.line_style = 0xffff;    /* line_style */
  311.  
  312.     if (lin < 0) {
  313.         return (-1);    /* 用心… */
  314.     }
  315.  
  316.     txxptr.y = (lin + 2) * 16;
  317.     TXXLINE(&txxptr);
  318.  
  319.     return (0);
  320. }
  321.  
  322. /* [ EOF ] */
  323.